home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / events.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  225 lines

  1. /*
  2.  * events.c
  3.  *
  4.  * This file is part of the basis of the Forms Library.
  5.  *
  6.  * It contains the routines that deal with keeping track of events.
  7.  * This includes all the replacement routines for the user event queue.
  8.  *
  9.  * Written by: Mark Overmars
  10.  *
  11.  * Version 2.1 c
  12.  * Date: Oct  6, 1992
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "gl/gl.h"
  17. #include "gl/device.h"
  18. #include "forms.h"
  19.  
  20. /*************** CALL-BACK ROUTINE HANDLING ***********/
  21.  
  22. static void (*fl_event_call_back)(short, short) = NULL;
  23.  
  24. void fl_set_event_call_back(FL_EVENTCALLBACKPTR call_back)
  25. /* Sets the call_back routine for the events */
  26.   { fl_event_call_back = call_back; }
  27.  
  28. void fl_set_call_back(FL_OBJECT *obj, FL_CALLBACKPTR call_back, long argument)
  29. /* Sets the call_back routine for the object */
  30. {
  31.    if (obj == NULL)
  32.      {fl_error("fl_set_call_back","Setting callback of NULL object."); return;}
  33.    obj->object_call_back = call_back;
  34.    obj->argument = argument;
  35. }
  36.  
  37. void fl_set_form_call_back(FL_FORM *form, FL_FORMCALLBACKPTR call_back)
  38. /* Sets the call_back routine for the form */
  39. {
  40.    if (form == NULL)
  41.      {fl_error("fl_set_form_call_back","Setting callback of NULL form."); return;}
  42.    form->form_call_back = call_back;
  43. }
  44.  
  45. /*************** THE NORMAL EVENTS ********************/
  46.  
  47. #define QSIZE    700
  48.  
  49. static int queued[MAXSGIDEVICE];    /* devices queued by user */
  50.  
  51. static short thedev[QSIZE];          /* The event queue */
  52. static short theval[QSIZE];
  53. static int head = 0 , tail = 0;
  54.  
  55. static int new_events = 0;        /* Number of not yet reported events */
  56.  
  57. static int special_event(Device dev)
  58. /* Returns whether this is a special event */
  59. {
  60.   return ( dev == MOUSE1 || dev == MOUSE2 ||dev == MOUSE3 ||
  61.      dev == INPUTCHANGE || dev == KEYBD || dev == LEFTARROWKEY ||
  62.      dev == RIGHTARROWKEY || dev == UPARROWKEY || dev == DOWNARROWKEY ||
  63.      dev == REDRAW || dev == WINQUIT ||
  64.          dev == WINFREEZE || dev == WINTHAW);
  65. }
  66.  
  67. void fl_init_events()
  68. /* initializes the event setting */
  69. {
  70.   int i;
  71.   for (i=0; i<MAXSGIDEVICE; i++) queued[i] = !special_event( (Device) i);
  72.   queued[REDRAW] = TRUE;
  73.   queued[INPUTCHANGE] = TRUE;
  74. }
  75.  
  76. void fl_qdevice(Device dev)
  77. /* queues a device */
  78. {
  79.   queued[dev] = TRUE;
  80.   qdevice(dev);
  81. }
  82.  
  83. void fl_unqdevice(Device dev)
  84. /* unqueues a device */
  85. {
  86.   if (special_event(dev)) queued[dev] = FALSE; else unqdevice(dev);
  87. }
  88.  
  89. int fl_isqueued(Device dev)
  90. /* test whether a device is queued */
  91. {
  92.   return (queued[dev] && isqueued(dev));
  93. }
  94.  
  95. long fl_qtest()
  96. /* Returns last queued event type */
  97. {
  98.   FL_OBJECT *obj;
  99.   if (head == tail)
  100.   {
  101.     fl_treat_interaction_events(FALSE);
  102.     fl_treat_user_events();
  103.   }
  104.   if (head == tail) return 0;
  105.   return  (long) thedev[tail];
  106. }
  107.  
  108. void fl_qenter(short qtype, short val)
  109. /* Adds an event to the queue */
  110. {
  111.   if (special_event( (Device) qtype) && !queued[qtype]) return;
  112.   new_events++;
  113.   if (head == tail-1 || (head == QSIZE-1 && tail == 0))
  114.     tail = (tail+1) % QSIZE;
  115.   thedev[head] = qtype;
  116.   theval[head] = val;
  117.   head = (head+1) % QSIZE;
  118. }
  119.  
  120. void fl_qreset()
  121. /* Empties the queue. */
  122.   fl_treat_interaction_events(FALSE); /* Get everything from the GL-queue. */
  123.   new_events = 0;
  124.   head = tail = 0;
  125. }
  126.  
  127. long fl_qread(short *data)
  128. /* reads an event from the queue. if empty, it waits for an event */
  129. {
  130.   short dev;
  131.   while (head == tail)
  132.   {
  133.     fl_treat_interaction_events(TRUE);
  134.     fl_treat_user_events();
  135.   }
  136.   dev = thedev[tail];
  137.   *data = theval[tail];
  138.   tail = (tail+1) % QSIZE;
  139.   return  (long) dev;
  140. }
  141.  
  142. long fl_blkqread(short *data,short n)
  143. {
  144.   long i = 0;
  145.   if (head == tail) return 0;
  146.   while (i < n-1 && head != tail)
  147.   { 
  148.     data[i++] = thedev[tail];
  149.     data[i++] = theval[tail];
  150.     tail = (tail+1) % QSIZE;
  151.   }
  152.   return i/2;
  153. }
  154.  
  155. void fl_tie(Device action, Device dev1, Device dev2)
  156. {
  157.   tie(action,dev1,dev2);
  158. }
  159.  
  160. void fl_treat_user_events()
  161. /* Treats all new user events, i.e., either calls the callback routine
  162.    or places FL_EVENT in the object queue. */
  163. {
  164.   short qtype,val;
  165.   int i;
  166.   if (new_events == 0) return;
  167.   if (fl_event_call_back != NULL)
  168.   {
  169.     fl_restore_user_window();
  170.     while (new_events>0)
  171.     {
  172.       new_events--;
  173.       qtype = (short) fl_qread(&val);
  174.       (*(fl_event_call_back))(qtype,val);      
  175.     }
  176.     fl_save_user_window();
  177.   }
  178.   else
  179.   {
  180.     for (i=0; i<new_events; i++) fl_object_qenter(FL_EVENT);
  181.     new_events = 0;
  182.   }
  183. }
  184.  
  185. /*************** THE OBJECT EVENTS ********************/
  186.  
  187. FL_OBJECT *FL_EVENT = (FL_OBJECT *) (-1);    /* The special event object */
  188.  
  189. static FL_OBJECT *theobj[QSIZE];          /* The object event queue */
  190. static int ohead = 0 , otail = 0;
  191.  
  192. void fl_object_qenter(FL_OBJECT *obj)
  193. /* Adds an object to the queue */
  194. {
  195.   if (ohead == otail-1 || (ohead == QSIZE-1 && otail == 0))
  196.     otail = (otail+1) % QSIZE;
  197.   theobj[ohead] = obj;
  198.   ohead = (ohead+1) % QSIZE;
  199. }
  200.  
  201. FL_OBJECT *fl_object_qread()
  202. /* reads an object from the queue.*/
  203. {
  204.   FL_OBJECT *obj;
  205.   if (ohead == otail) return NULL;
  206.   obj = theobj[otail];
  207.   otail = (otail+1) % QSIZE;
  208.   if (obj != FL_EVENT && obj->object_call_back != NULL)
  209.   { 
  210.     fl_restore_user_window();
  211.     (*(obj->object_call_back))(obj,obj->argument);
  212.     fl_save_user_window();
  213.     return NULL;
  214.   }
  215.   else if (obj != FL_EVENT && obj->form->form_call_back != NULL)
  216.   { 
  217.     fl_restore_user_window();
  218.     (*(obj->form->form_call_back))(obj);
  219.     fl_save_user_window();
  220.     return NULL;
  221.   }
  222.   return obj;
  223. }
  224.